package files;

import java.io.*;
import java.util.TreeSet;

/**
 * Created by alone on 2018/10/24.
 *
 * @author lingfengsan
 */
public class Main {
    private static BufferedWriter bw;
    private static TreeSet<Character> INVALID_CHAR = new TreeSet<>();
    private static String dest;
    private static char[] INVALID_CHARS = {'，', '-', ' ', '：', '“', '”', '《', '》'};
    private static String UNIVERSAL="ffmpeg -i \"%s\" -c:v hevc_nvenc -c:a copy  -r 25 \"%smp4\"";

    public static void main(String[] args) {
        initial();
        generateBat("D:\\bt1\\0809\\Ai Uehara MegaPack\\Videos\\no watermark");
    }

    private static void initial() {
        for (int i = 0; i < INVALID_CHARS.length; i++) {
            INVALID_CHAR.add(INVALID_CHARS[i]);
        }
    }

    private static void generateBat(String path) {
        File file = new File(path);
        dest = path + "_hevc\\";
        String batName = path + "\\process.bat";
        File batFile = new File(batName);
        try {
            if (batFile.exists()) {
                batFile.delete();
            }
            batFile.createNewFile();
            FileWriter fw = new FileWriter(batFile, true);
            bw = new BufferedWriter(fw);
        } catch (IOException e) {
            System.out.println(batName + "file not exist!!");
        }
        printFiles(file);
        try {
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void printFiles(File file) {
        if (file.isDirectory()) {
            if (file.listFiles() == null) return;
            for (File file1 : file.listFiles()) {
                printFiles(file1);
            }
        } else {
            String str = file.getAbsolutePath();
            if (str.endsWith(".mkv") || str.endsWith(".mp4") || str.endsWith(".wmv")|| str.endsWith(".avi")) {
//                file.renameTo(new File(file.getParentFile().getAbsolutePath()+"\\"+delteSpecialChar(file.getName())));
                try {
                    String fileName = file.getName();
                    String cmd=String.format(UNIVERSAL,str,dest +"lfs_" +fileName.substring(0,fileName.length()-3));
                    System.out.println(cmd);
                    bw.write(cmd);
                    bw.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static String delteSpecialChar(String str) {
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            if (valid(str.charAt(i))) ans.append(str.charAt(i));
        }
        return ans.toString();
    }

    private static boolean valid(char c) {
        return !INVALID_CHAR.contains(c);
    }
}
